<?php

declare(strict_types=1);

namespace App\CustHub\Controller;



use App\Common\Service\AliyunService;
use App\Common\Service\SmsService;
use App\CustHub\Request\SmsRequest;
use App\CustHub\Service\ChUserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/sms')]
class SmsController extends BaseController
{
    #[Inject]
    protected ChUserService $userService;

    /**
     * 发送验证码
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('sendCode')]
    public function sendCode(SmsRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $isTest = env('IS_LOCAL',false);
        $captchaVerifyParam = $data['captchaVerifyParam'] ?? '';
        $result = (new AliyunService())->newSlide($captchaVerifyParam);
        if(!$result){
            return $this->error('滑动验证失败');
        }
        $smsService = make(SmsService::class,[$data['event']]);
        $result = $smsService->sendCode($data['mobile'],$isTest);
        $msg = $isTest ? (string)$result : '验证码发送成功';
        return $result!== false ? $this->success($msg) : $this->error('验证码发送失败');
    }

    /**
     * 检查手机号是否存在
     * @param SmsRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('checkMobile')]
    public function checkMobile(SmsRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        return  $this->success('手机号校验',['exists'=>false]);
//        return  $this->success('手机号校验',['exists'=>$this->userService->checkUserMobileExist($data['mobile'])]);
    }
}